

<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>federatedml.feature.imputer &mdash; FATE 1.0 documentation</title>
  

  
  
  
  

  
  <script type="text/javascript" src="../../../_static/js/modernizr.min.js"></script>
  
    
      <script type="text/javascript" id="documentation_options" data-url_root="../../../" src="../../../_static/documentation_options.js"></script>
        <script type="text/javascript" src="../../../_static/jquery.js"></script>
        <script type="text/javascript" src="../../../_static/underscore.js"></script>
        <script type="text/javascript" src="../../../_static/doctools.js"></script>
        <script type="text/javascript" src="../../../_static/language_data.js"></script>
    
    <script type="text/javascript" src="../../../_static/js/theme.js"></script>

    

  
  <link rel="stylesheet" href="../../../_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" />
    <link rel="index" title="Index" href="../../../genindex.html" />
    <link rel="search" title="Search" href="../../../search.html" /> 
</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">
    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >
          

          
            <a href="../../../index.html" class="icon icon-home"> FATE
          

          
          </a>

          
            
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <!-- Local TOC -->
              <div class="local-toc"></div>
            
          
        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../../../index.html">FATE</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../../../index.html">Docs</a> &raquo;</li>
        
          <li><a href="../../index.html">Module code</a> &raquo;</li>
        
      <li>federatedml.feature.imputer</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <h1>Source code for federatedml.feature.imputer</h1><div class="highlight"><pre>
<span></span><span class="kn">import</span> <span class="nn">functools</span>
<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>

<span class="kn">from</span> <span class="nn">arch.api.utils</span> <span class="k">import</span> <span class="n">log_utils</span>
<span class="kn">from</span> <span class="nn">federatedml.statistic.data_overview</span> <span class="k">import</span> <span class="n">get_header</span>
<span class="kn">from</span> <span class="nn">federatedml.statistic.statics</span> <span class="k">import</span> <span class="n">MultivariateStatisticalSummary</span>
<span class="kn">from</span> <span class="nn">federatedml.util</span> <span class="k">import</span> <span class="n">consts</span>
<span class="kn">from</span> <span class="nn">federatedml.statistic</span> <span class="k">import</span> <span class="n">data_overview</span>

<span class="n">LOGGER</span> <span class="o">=</span> <span class="n">log_utils</span><span class="o">.</span><span class="n">getLogger</span><span class="p">()</span>


<div class="viewcode-block" id="Imputer"><a class="viewcode-back" href="../../../federatedml.feature.html#federatedml.feature.imputer.Imputer">[docs]</a><span class="k">class</span> <span class="nc">Imputer</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    This class provides basic strategies for values replacement. It can be used as missing filled or outlier replace.</span>
<span class="sd">    You can use the statistics such as mean, median or max of each column to fill the missing value or replace outlier.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">missing_value_list</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        missing_value_list: list of str, the value to be replaced. Default None, if is None, it will be set to list of blank, none, null and na,</span>
<span class="sd">                            which regarded as missing filled. If not, it can be outlier replace, and missing_value_list includes the outlier values</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="n">missing_value_list</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">missing_value_list</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;&#39;</span><span class="p">,</span> <span class="s1">&#39;none&#39;</span><span class="p">,</span> <span class="s1">&#39;null&#39;</span><span class="p">,</span> <span class="s1">&#39;na&#39;</span><span class="p">]</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">missing_value_list</span> <span class="o">=</span> <span class="n">missing_value_list</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">support_replace_method</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;min&#39;</span><span class="p">,</span> <span class="s1">&#39;max&#39;</span><span class="p">,</span> <span class="s1">&#39;mean&#39;</span><span class="p">,</span> <span class="s1">&#39;median&#39;</span><span class="p">,</span> <span class="s1">&#39;quantile&#39;</span><span class="p">,</span> <span class="s1">&#39;designated&#39;</span><span class="p">]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">support_output_format</span> <span class="o">=</span> <span class="p">{</span>
            <span class="s1">&#39;str&#39;</span><span class="p">:</span> <span class="nb">str</span><span class="p">,</span>
            <span class="s1">&#39;float&#39;</span><span class="p">:</span> <span class="nb">float</span><span class="p">,</span>
            <span class="s1">&#39;int&#39;</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span>
            <span class="s1">&#39;origin&#39;</span><span class="p">:</span> <span class="kc">None</span>
        <span class="p">}</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">support_replace_area</span> <span class="o">=</span> <span class="p">{</span>
            <span class="s1">&#39;min&#39;</span><span class="p">:</span> <span class="s1">&#39;col&#39;</span><span class="p">,</span>
            <span class="s1">&#39;max&#39;</span><span class="p">:</span> <span class="s1">&#39;col&#39;</span><span class="p">,</span>
            <span class="s1">&#39;mean&#39;</span><span class="p">:</span> <span class="s1">&#39;col&#39;</span><span class="p">,</span>
            <span class="s1">&#39;median&#39;</span><span class="p">:</span> <span class="s1">&#39;col&#39;</span><span class="p">,</span>
            <span class="s1">&#39;quantile&#39;</span><span class="p">:</span> <span class="s1">&#39;col&#39;</span><span class="p">,</span>
            <span class="s1">&#39;designated&#39;</span><span class="p">:</span> <span class="s1">&#39;col&#39;</span>
        <span class="p">}</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">cols_fit_impute_rate</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">cols_transform_impute_rate</span> <span class="o">=</span> <span class="p">[]</span>

<div class="viewcode-block" id="Imputer.get_missing_value_list"><a class="viewcode-back" href="../../../federatedml.feature.html#federatedml.feature.imputer.Imputer.get_missing_value_list">[docs]</a>    <span class="k">def</span> <span class="nf">get_missing_value_list</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">missing_value_list</span></div>

<div class="viewcode-block" id="Imputer.get_impute_rate"><a class="viewcode-back" href="../../../federatedml.feature.html#federatedml.feature.imputer.Imputer.get_impute_rate">[docs]</a>    <span class="k">def</span> <span class="nf">get_impute_rate</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">mode</span><span class="o">=</span><span class="s2">&quot;fit&quot;</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">mode</span> <span class="o">==</span> <span class="s2">&quot;fit&quot;</span><span class="p">:</span>
            <span class="k">return</span> <span class="nb">list</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">cols_fit_impute_rate</span><span class="p">)</span>
        <span class="k">elif</span> <span class="n">mode</span> <span class="o">==</span> <span class="s2">&quot;transform&quot;</span><span class="p">:</span>
            <span class="k">return</span> <span class="nb">list</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">cols_transform_impute_rate</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;Unknown mode of </span><span class="si">{}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">mode</span><span class="p">))</span></div>

    <span class="nd">@staticmethod</span>
    <span class="k">def</span> <span class="nf">__replace_missing_value_with_cols_transform_value_format</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="n">transform_list</span><span class="p">,</span> <span class="n">missing_value_list</span><span class="p">,</span>
                                                                 <span class="n">output_format</span><span class="p">):</span>
        <span class="n">replace_cols_index_list</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">i</span><span class="p">,</span> <span class="n">v</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">data</span><span class="p">):</span>
            <span class="k">if</span> <span class="nb">str</span><span class="p">(</span><span class="n">v</span><span class="p">)</span> <span class="ow">in</span> <span class="n">missing_value_list</span><span class="p">:</span>
                <span class="n">data</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="n">output_format</span><span class="p">(</span><span class="n">transform_list</span><span class="p">[</span><span class="n">i</span><span class="p">])</span>
                <span class="n">replace_cols_index_list</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">i</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">data</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="n">output_format</span><span class="p">(</span><span class="n">v</span><span class="p">)</span>

        <span class="k">return</span> <span class="n">data</span><span class="p">,</span> <span class="n">replace_cols_index_list</span>

    <span class="nd">@staticmethod</span>
    <span class="k">def</span> <span class="nf">__replace_missing_value_with_cols_transform_value</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="n">transform_list</span><span class="p">,</span> <span class="n">missing_value_list</span><span class="p">):</span>
        <span class="n">replace_cols_index_list</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">i</span><span class="p">,</span> <span class="n">v</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">data</span><span class="p">):</span>
            <span class="k">if</span> <span class="nb">str</span><span class="p">(</span><span class="n">v</span><span class="p">)</span> <span class="ow">in</span> <span class="n">missing_value_list</span><span class="p">:</span>
                <span class="n">data</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="nb">str</span><span class="p">(</span><span class="n">transform_list</span><span class="p">[</span><span class="n">i</span><span class="p">])</span>
                <span class="n">replace_cols_index_list</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">i</span><span class="p">)</span>

        <span class="k">return</span> <span class="n">data</span><span class="p">,</span> <span class="n">replace_cols_index_list</span>

    <span class="nd">@staticmethod</span>
    <span class="k">def</span> <span class="nf">__replace_missing_value_with_replace_value_format</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="n">replace_value</span><span class="p">,</span> <span class="n">missing_value_list</span><span class="p">,</span> <span class="n">output_format</span><span class="p">):</span>
        <span class="n">replace_cols_index_list</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">i</span><span class="p">,</span> <span class="n">v</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">data</span><span class="p">):</span>
            <span class="k">if</span> <span class="nb">str</span><span class="p">(</span><span class="n">v</span><span class="p">)</span> <span class="ow">in</span> <span class="n">missing_value_list</span><span class="p">:</span>
                <span class="n">data</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="n">output_format</span><span class="p">(</span><span class="n">replace_value</span><span class="p">)</span>
                <span class="n">replace_cols_index_list</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">i</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">data</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="n">output_format</span><span class="p">(</span><span class="n">data</span><span class="p">[</span><span class="n">i</span><span class="p">])</span>

        <span class="k">return</span> <span class="n">data</span><span class="p">,</span> <span class="n">replace_cols_index_list</span>

    <span class="nd">@staticmethod</span>
    <span class="k">def</span> <span class="nf">__replace_missing_value_with_replace_value</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="n">replace_value</span><span class="p">,</span> <span class="n">missing_value_list</span><span class="p">):</span>
        <span class="n">replace_cols_index_list</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">i</span><span class="p">,</span> <span class="n">v</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">data</span><span class="p">):</span>
            <span class="k">if</span> <span class="nb">str</span><span class="p">(</span><span class="n">v</span><span class="p">)</span> <span class="ow">in</span> <span class="n">missing_value_list</span><span class="p">:</span>
                <span class="n">data</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="nb">str</span><span class="p">(</span><span class="n">replace_value</span><span class="p">)</span>
                <span class="n">replace_cols_index_list</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">i</span><span class="p">)</span>

        <span class="k">return</span> <span class="n">data</span><span class="p">,</span> <span class="n">replace_cols_index_list</span>

    <span class="k">def</span> <span class="nf">__get_cols_transform_value</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="n">replace_method</span><span class="p">,</span> <span class="n">quantile</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="n">summary_obj</span> <span class="o">=</span> <span class="n">MultivariateStatisticalSummary</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">,</span> <span class="n">abnormal_list</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">missing_value_list</span><span class="p">)</span>
        <span class="n">header</span> <span class="o">=</span> <span class="n">get_header</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>

        <span class="k">if</span> <span class="n">replace_method</span> <span class="o">==</span> <span class="n">consts</span><span class="o">.</span><span class="n">MIN</span><span class="p">:</span>
            <span class="n">cols_transform_value</span> <span class="o">=</span> <span class="n">summary_obj</span><span class="o">.</span><span class="n">get_min</span><span class="p">()</span>
        <span class="k">elif</span> <span class="n">replace_method</span> <span class="o">==</span> <span class="n">consts</span><span class="o">.</span><span class="n">MAX</span><span class="p">:</span>
            <span class="n">cols_transform_value</span> <span class="o">=</span> <span class="n">summary_obj</span><span class="o">.</span><span class="n">get_max</span><span class="p">()</span>
        <span class="k">elif</span> <span class="n">replace_method</span> <span class="o">==</span> <span class="n">consts</span><span class="o">.</span><span class="n">MEAN</span><span class="p">:</span>
            <span class="n">cols_transform_value</span> <span class="o">=</span> <span class="n">summary_obj</span><span class="o">.</span><span class="n">get_mean</span><span class="p">()</span>
        <span class="k">elif</span> <span class="n">replace_method</span> <span class="o">==</span> <span class="n">consts</span><span class="o">.</span><span class="n">MEDIAN</span><span class="p">:</span>
            <span class="n">cols_transform_value</span> <span class="o">=</span> <span class="n">summary_obj</span><span class="o">.</span><span class="n">get_median</span><span class="p">()</span>
        <span class="k">elif</span> <span class="n">replace_method</span> <span class="o">==</span> <span class="n">consts</span><span class="o">.</span><span class="n">QUANTILE</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">quantile</span> <span class="o">&gt;</span> <span class="mi">1</span> <span class="ow">or</span> <span class="n">quantile</span> <span class="o">&lt;</span> <span class="mi">0</span><span class="p">:</span>
                <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;quantile should between 0 and 1, but get:</span><span class="si">{}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">quantile</span><span class="p">))</span>
            <span class="n">cols_transform_value</span> <span class="o">=</span> <span class="n">summary_obj</span><span class="o">.</span><span class="n">get_quantile_point</span><span class="p">(</span><span class="n">quantile</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;Unknown replace method:</span><span class="si">{}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">replace_method</span><span class="p">))</span>

        <span class="n">cols_transform_value</span> <span class="o">=</span> <span class="p">[</span><span class="nb">round</span><span class="p">(</span><span class="n">cols_transform_value</span><span class="p">[</span><span class="n">key</span><span class="p">],</span> <span class="mi">6</span><span class="p">)</span> <span class="k">for</span> <span class="n">key</span> <span class="ow">in</span> <span class="n">header</span><span class="p">]</span>
        <span class="k">return</span> <span class="n">cols_transform_value</span>

    <span class="k">def</span> <span class="nf">__fit_replace</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="n">replace_method</span><span class="p">,</span> <span class="n">replace_value</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">output_format</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">quantile</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">replace_method</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">replace_method</span> <span class="o">!=</span> <span class="n">consts</span><span class="o">.</span><span class="n">DESIGNATED</span><span class="p">:</span>
            <span class="n">cols_transform_value</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">__get_cols_transform_value</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="n">replace_method</span><span class="p">,</span> <span class="n">quantile</span><span class="o">=</span><span class="n">quantile</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">output_format</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
                <span class="n">f</span> <span class="o">=</span> <span class="n">functools</span><span class="o">.</span><span class="n">partial</span><span class="p">(</span><span class="n">Imputer</span><span class="o">.</span><span class="n">__replace_missing_value_with_cols_transform_value_format</span><span class="p">,</span>
                                      <span class="n">transform_list</span><span class="o">=</span><span class="n">cols_transform_value</span><span class="p">,</span> <span class="n">missing_value_list</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">missing_value_list</span><span class="p">,</span>
                                      <span class="n">output_format</span><span class="o">=</span><span class="n">output_format</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">f</span> <span class="o">=</span> <span class="n">functools</span><span class="o">.</span><span class="n">partial</span><span class="p">(</span><span class="n">Imputer</span><span class="o">.</span><span class="n">__replace_missing_value_with_cols_transform_value</span><span class="p">,</span>
                                      <span class="n">transform_list</span><span class="o">=</span><span class="n">cols_transform_value</span><span class="p">,</span> <span class="n">missing_value_list</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">missing_value_list</span><span class="p">)</span>

            <span class="n">transform_data</span> <span class="o">=</span> <span class="n">data</span><span class="o">.</span><span class="n">mapValues</span><span class="p">(</span><span class="n">f</span><span class="p">)</span>
            <span class="n">LOGGER</span><span class="o">.</span><span class="n">info</span><span class="p">(</span>
                <span class="s2">&quot;finish replace missing value with cols transform value, replace method is </span><span class="si">{}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">replace_method</span><span class="p">))</span>
            <span class="k">return</span> <span class="n">transform_data</span><span class="p">,</span> <span class="n">cols_transform_value</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">replace_value</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
                <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;Replace value should not be None&quot;</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">output_format</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
                <span class="n">f</span> <span class="o">=</span> <span class="n">functools</span><span class="o">.</span><span class="n">partial</span><span class="p">(</span><span class="n">Imputer</span><span class="o">.</span><span class="n">__replace_missing_value_with_replace_value_format</span><span class="p">,</span>
                                      <span class="n">replace_value</span><span class="o">=</span><span class="n">replace_value</span><span class="p">,</span> <span class="n">missing_value_list</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">missing_value_list</span><span class="p">,</span>
                                      <span class="n">output_format</span><span class="o">=</span><span class="n">output_format</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">f</span> <span class="o">=</span> <span class="n">functools</span><span class="o">.</span><span class="n">partial</span><span class="p">(</span><span class="n">Imputer</span><span class="o">.</span><span class="n">__replace_missing_value_with_replace_value</span><span class="p">,</span> <span class="n">replace_value</span><span class="o">=</span><span class="n">replace_value</span><span class="p">,</span>
                                      <span class="n">missing_value_list</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">missing_value_list</span><span class="p">)</span>
            <span class="n">transform_data</span> <span class="o">=</span> <span class="n">data</span><span class="o">.</span><span class="n">mapValues</span><span class="p">(</span><span class="n">f</span><span class="p">)</span>
            <span class="n">LOGGER</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s2">&quot;finish replace missing value with replace value </span><span class="si">{}</span><span class="s2">, replace method is:</span><span class="si">{}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">replace_value</span><span class="p">,</span>
                                                                                                          <span class="n">replace_method</span><span class="p">))</span>
            <span class="n">shape</span> <span class="o">=</span> <span class="n">data_overview</span><span class="o">.</span><span class="n">get_data_shape</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
            <span class="n">replace_value</span> <span class="o">=</span> <span class="p">[</span><span class="n">replace_value</span> <span class="k">for</span> <span class="n">_</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">shape</span><span class="p">)]</span>

            <span class="k">return</span> <span class="n">transform_data</span><span class="p">,</span> <span class="n">replace_value</span>

    <span class="k">def</span> <span class="nf">__transform_replace</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="n">transform_value</span><span class="p">,</span> <span class="n">replace_area</span><span class="p">,</span> <span class="n">output_format</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">replace_area</span> <span class="o">==</span> <span class="s1">&#39;all&#39;</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">output_format</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
                <span class="n">f</span> <span class="o">=</span> <span class="n">functools</span><span class="o">.</span><span class="n">partial</span><span class="p">(</span><span class="n">Imputer</span><span class="o">.</span><span class="n">__replace_missing_value_with_replace_value_format</span><span class="p">,</span>
                                      <span class="n">replace_value</span><span class="o">=</span><span class="n">transform_value</span><span class="p">,</span> <span class="n">missing_value_list</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">missing_value_list</span><span class="p">,</span>
                                      <span class="n">output_format</span><span class="o">=</span><span class="n">output_format</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">f</span> <span class="o">=</span> <span class="n">functools</span><span class="o">.</span><span class="n">partial</span><span class="p">(</span><span class="n">Imputer</span><span class="o">.</span><span class="n">__replace_missing_value_with_replace_value</span><span class="p">,</span>
                                      <span class="n">replace_value</span><span class="o">=</span><span class="n">transform_value</span><span class="p">,</span> <span class="n">missing_value_list</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">missing_value_list</span><span class="p">)</span>
        <span class="k">elif</span> <span class="n">replace_area</span> <span class="o">==</span> <span class="s1">&#39;col&#39;</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">output_format</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
                <span class="n">f</span> <span class="o">=</span> <span class="n">functools</span><span class="o">.</span><span class="n">partial</span><span class="p">(</span><span class="n">Imputer</span><span class="o">.</span><span class="n">__replace_missing_value_with_cols_transform_value_format</span><span class="p">,</span>
                                      <span class="n">transform_list</span><span class="o">=</span><span class="n">transform_value</span><span class="p">,</span> <span class="n">missing_value_list</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">missing_value_list</span><span class="p">,</span>
                                      <span class="n">output_format</span><span class="o">=</span><span class="n">output_format</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">f</span> <span class="o">=</span> <span class="n">functools</span><span class="o">.</span><span class="n">partial</span><span class="p">(</span><span class="n">Imputer</span><span class="o">.</span><span class="n">__replace_missing_value_with_cols_transform_value</span><span class="p">,</span>
                                      <span class="n">transform_list</span><span class="o">=</span><span class="n">transform_value</span><span class="p">,</span> <span class="n">missing_value_list</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">missing_value_list</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;Unknown replace area </span><span class="si">{}</span><span class="s2"> in Imputer&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">replace_area</span><span class="p">))</span>

        <span class="n">transform_data</span> <span class="o">=</span> <span class="n">data</span><span class="o">.</span><span class="n">mapValues</span><span class="p">(</span><span class="n">f</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">transform_data</span>

    <span class="nd">@staticmethod</span>
    <span class="k">def</span> <span class="nf">__get_impute_number</span><span class="p">(</span><span class="n">some_data</span><span class="p">):</span>
        <span class="n">impute_num_list</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="n">data_size</span> <span class="o">=</span> <span class="kc">None</span>

        <span class="k">for</span> <span class="n">line</span> <span class="ow">in</span> <span class="n">some_data</span><span class="p">:</span>
            <span class="n">processed_data</span> <span class="o">=</span> <span class="n">line</span><span class="p">[</span><span class="mi">1</span><span class="p">][</span><span class="mi">0</span><span class="p">]</span>
            <span class="n">index_list</span> <span class="o">=</span> <span class="n">line</span><span class="p">[</span><span class="mi">1</span><span class="p">][</span><span class="mi">1</span><span class="p">]</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="n">data_size</span><span class="p">:</span>
                <span class="n">data_size</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">processed_data</span><span class="p">)</span>
                <span class="c1"># data_size + 1, the last element of impute_num_list used to count the number of &quot;some_data&quot;</span>
                <span class="n">impute_num_list</span> <span class="o">=</span> <span class="p">[</span><span class="mi">0</span> <span class="k">for</span> <span class="n">_</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">data_size</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)]</span>

            <span class="n">impute_num_list</span><span class="p">[</span><span class="n">data_size</span><span class="p">]</span> <span class="o">+=</span> <span class="mi">1</span>
            <span class="k">for</span> <span class="n">index</span> <span class="ow">in</span> <span class="n">index_list</span><span class="p">:</span>
                <span class="n">impute_num_list</span><span class="p">[</span><span class="n">index</span><span class="p">]</span> <span class="o">+=</span> <span class="mi">1</span>

        <span class="k">return</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">impute_num_list</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">__get_impute_rate_from_replace_data</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">):</span>
        <span class="n">impute_number_statics</span> <span class="o">=</span> <span class="n">data</span><span class="o">.</span><span class="n">mapPartitions</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">__get_impute_number</span><span class="p">)</span><span class="o">.</span><span class="n">reduce</span><span class="p">(</span><span class="k">lambda</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">:</span> <span class="n">x</span> <span class="o">+</span> <span class="n">y</span><span class="p">)</span>
        <span class="n">cols_impute_rate</span> <span class="o">=</span> <span class="n">impute_number_statics</span><span class="p">[:</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="o">/</span> <span class="n">impute_number_statics</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>

        <span class="k">return</span> <span class="n">cols_impute_rate</span>

<div class="viewcode-block" id="Imputer.fit"><a class="viewcode-back" href="../../../federatedml.feature.html#federatedml.feature.imputer.Imputer.fit">[docs]</a>    <span class="k">def</span> <span class="nf">fit</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="n">replace_method</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">replace_value</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">output_format</span><span class="o">=</span><span class="n">consts</span><span class="o">.</span><span class="n">ORIGIN</span><span class="p">,</span> <span class="n">quantile</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Apply imputer for input data</span>
<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        data: DTable, each data&#39;s value should be list</span>
<span class="sd">        replace_method: str, the strategy of imputer, like min, max, mean or designated and so on. Default None</span>
<span class="sd">        replace_value: str, if replace_method is designated, you should assign the replace_value which will be used to replace the value in imputer_value_list</span>
<span class="sd">        output_format: str, the output data format. The output data can be &#39;str&#39;, &#39;int&#39;, &#39;float&#39;. Default origin, the original format as input data</span>

<span class="sd">        Returns</span>
<span class="sd">        ----------</span>
<span class="sd">        fit_data:data_instance, data after imputer</span>
<span class="sd">        cols_transform_value: list, the replace value in each column</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="n">output_format</span> <span class="ow">not</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">support_output_format</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;Unsupport output_format:</span><span class="si">{}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">output_format</span><span class="p">))</span>

        <span class="n">output_format</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">support_output_format</span><span class="p">[</span><span class="n">output_format</span><span class="p">]</span>

        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">replace_method</span><span class="p">,</span> <span class="nb">str</span><span class="p">):</span>
            <span class="n">replace_method</span> <span class="o">=</span> <span class="n">replace_method</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span>
            <span class="k">if</span> <span class="n">replace_method</span> <span class="ow">not</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">support_replace_method</span><span class="p">:</span>
                <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;Unknown replace method:</span><span class="si">{}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">replace_method</span><span class="p">))</span>
        <span class="k">elif</span> <span class="n">replace_method</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">replace_value</span> <span class="o">=</span> <span class="s1">&#39;0&#39;</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;parameter replace_method should be str or None only&quot;</span><span class="p">)</span>

        <span class="n">process_data</span><span class="p">,</span> <span class="n">cols_transform_value</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">__fit_replace</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="n">replace_method</span><span class="p">,</span> <span class="n">replace_value</span><span class="p">,</span> <span class="n">output_format</span><span class="p">,</span>
                                                                <span class="n">quantile</span><span class="o">=</span><span class="n">quantile</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">cols_fit_impute_rate</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">__get_impute_rate_from_replace_data</span><span class="p">(</span><span class="n">process_data</span><span class="p">)</span>
        <span class="n">process_data</span> <span class="o">=</span> <span class="n">process_data</span><span class="o">.</span><span class="n">mapValues</span><span class="p">(</span><span class="k">lambda</span> <span class="n">v</span><span class="p">:</span><span class="n">v</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span>

        <span class="k">return</span> <span class="n">process_data</span><span class="p">,</span> <span class="n">cols_transform_value</span></div>

<div class="viewcode-block" id="Imputer.transform"><a class="viewcode-back" href="../../../federatedml.feature.html#federatedml.feature.imputer.Imputer.transform">[docs]</a>    <span class="k">def</span> <span class="nf">transform</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="n">transform_value</span><span class="p">,</span> <span class="n">output_format</span><span class="o">=</span><span class="n">consts</span><span class="o">.</span><span class="n">ORIGIN</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Transform input data using Imputer with fit results</span>
<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        data: DTable, each data&#39;s value should be list</span>
<span class="sd">        output_format: str, the output data format. The output data can be &#39;str&#39;, &#39;int&#39;, &#39;float&#39;. Default origin, the original format as input data</span>

<span class="sd">        Returns</span>
<span class="sd">        ----------</span>
<span class="sd">        transform_data:data_instance, data after transform</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="n">output_format</span> <span class="ow">not</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">support_output_format</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;Unsupport output_format:</span><span class="si">{}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">output_format</span><span class="p">))</span>

        <span class="n">output_format</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">support_output_format</span><span class="p">[</span><span class="n">output_format</span><span class="p">]</span>

        <span class="c1"># Now all of replace_method is &quot;col&quot;, remain replace_area temporarily</span>
        <span class="c1"># replace_area = self.support_replace_area[replace_method]</span>
        <span class="n">replace_area</span> <span class="o">=</span> <span class="s2">&quot;col&quot;</span>
        <span class="n">process_data</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">__transform_replace</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="n">transform_value</span><span class="p">,</span> <span class="n">replace_area</span><span class="p">,</span> <span class="n">output_format</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">cols_transform_impute_rate</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">__get_impute_rate_from_replace_data</span><span class="p">(</span><span class="n">process_data</span><span class="p">)</span>
        <span class="n">process_data</span> <span class="o">=</span> <span class="n">process_data</span><span class="o">.</span><span class="n">mapValues</span><span class="p">(</span><span class="k">lambda</span> <span class="n">v</span><span class="p">:</span> <span class="n">v</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span>

        <span class="k">return</span> <span class="n">process_data</span></div></div>
</pre></div>

           </div>
           
          </div>
          <footer>
  

  <hr/>

  <div role="contentinfo">
    <p>
        &copy; Copyright 2019, FATE_TEAM

    </p>
  </div>
  Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>. 

</footer>

        </div>
      </div>

    </section>

  </div>
  


  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script>

  
  
    
   

</body>
</html>